package request

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"wrblog-api-go/pkg/client"
	"wrblog-api-go/pkg/result"
	"wrblog-api-go/pkg/status"
)

const BindObj = "bind_obj"

func BindAllStruct[T any]() gin.HandlerFunc {
	return func(c *gin.Context) {
		var obj T
		// 优先绑定 URI
		_ = c.ShouldBindUri(&obj)
		// 然后绑定 Query / Form / JSON
		if err := c.ShouldBind(&obj); err != nil {
			result.Wrap(status.BadRequest, fmt.Sprintf("参数解析失败:%s", err.Error()))
			c.Abort()
			return
		}
		c.Set(BindObj, obj)
		c.Set("clientInfo", client.GetClient(c))
		c.Next()
	}
}
